﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csvm2.message.contract;
using csvm2.pin.contract;
using ebc.componentbinder;
using ebc.patterns;

namespace csvm2.coordinator
{
    public class Coordinator : ICoordinator
    {
        private readonly IFileAdapter fileadapter;
        private readonly IPageBuilder pagebuilder;


        public Coordinator(IFileAdapter fileadapter, IRecordBuilder recordbuilder, IPageBuilder pagebuilder)
        {
            this.fileadapter = fileadapter;
            this.pagebuilder = pagebuilder;

            new ComponentBinder().Bind(fileadapter, recordbuilder);

            var hts = new HeadTailSplitter<Record>(rec => rec == null);
            recordbuilder.OnRecord += hts.ProcessList;
            hts.OnHead += header => this.OnHeader(header);
            hts.OnTail += this.pagebuilder.ProcessRecord;
        }


        public void ProcessLoad(string filename)
        {
            this.fileadapter.ProcessLoad(filename);
        }


        public event Action<Record> OnHeader;
        
        public event Action<Page> OnPage
        {
            add { this.pagebuilder.OnPage += value; }
            remove { this.pagebuilder.OnPage -= value; }
        }


        public event Action<double> OnProgress
        {
            add { this.fileadapter.OnProgress += value; }
            remove { this.fileadapter.OnProgress -= value; }
        }
    }
}
